const app = getApp()
import util from '../../../utils/util'
import {
  ServiceMenu,
  SelectMember,
  UpVolunt,
  GetTemplateConfig
} from '../../../config/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    model: 1,
    member: '', // 用户会员信息
    curIndex: 0,
    isShowPopup: false, // 是否显示姓名弹出
    allName: [],
    tid: '',
    tname: '',
    uid: '',
    // catpid: '',
    //  cat_id: '',
    content: '',
    mobile:'',

    type: 1,
    pid: 0,
    allMenu: [],
    catArr: [], // 栏目id数组
    multiObjArray: [
      [],
      []
    ],
    multiIndex: [0, 0],
    //isChecked:false,
    showView: true,
    tmplIds: [], // 模板消息id集合
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let model = wx.getStorageSync('model')
    if (model) {
      this.setData({
        model
      })
    }
    let member = wx.getStorageSync('member')
    let grider_storge = wx.getStorageSync('grider')

    if (member) {
      this.setData({
        member
      })
    } else if (grider_storge && !member) {
      wx.showModal({
        showCancel: false,
        content: '您暂不能操作此功能',
        success(res) {
          if (res.confirm) {
            wx.redirectTo({
              url: '/pages/index/index',
            })
          }
        }
      })
      return;
    } else {
      wx.redirectTo({
        url: '/pages/register/register',
      })
      return;
    }
    
    let uid = wx.getStorageSync('member').id;
    console.log('uid', uid)
    this.getServiceMenu();
    this.setData({
      uid: uid
    });

    this.getTmplIds();
  },

  onSelectPerson(e) {
    console.log('e', e)
    let index = e.currentTarget.dataset.index;
    this.setData({
      curIndex: index
    })
    if (index == 0) {
      this.setData({
        isShowPopup: false
      })
    }
  },

  onSearchName(e) {
    console.log(e)
    let name = e.detail.value;
    console.log('name', name)
    if (name != '') {
      this.setData({
        isShowPopup: true
      })
      util.apiRequest(SelectMember, {
        name: name,
        vollege_id: app.globalData.vollege_id
      }, null).then(r => {
        console.log('搜索：', r)
        this.setData({
          allName: r.data.data
        })
      })
    } else {
      this.setData({
        tname: '',
        isShowPopup: false
      })
    }
  },

  onConfirmName(e) {
    console.log('选择姓名', e)
    let tid = e.currentTarget.dataset.id;
    let tname = e.currentTarget.dataset.name;
    this.setData({
      tid: tid,
      tname: tname,
      isShowPopup: false
    })
  },

  chose() {
    this.setData({
      showView: false,
      catArr: []
    })
    this.getServiceMenu();
  },

  getServiceMenu() {
    let that = this;
    let params = {
      type: this.data.type,
      pid: this.data.pid,
      vollege_id: app.globalData.vollege_id
    }
    util.apiRequest(ServiceMenu, params, null).then(r => {
      console.log('服务菜单：', r)
      that.setData({
        // 'multiObjArray[0]': r.data.data,
        allMenu: r.data.data,
        //catpid: r.data.data[0].id
      })

      // let pid = r.data.data[0].id;
      // let params = {
      //   type: this.data.type,
      //   pid: pid
      // }
      // console.log('二级参数', params)
      // util.apiRequest(ServiceMenu, params, null).then(r => {
      //   console.log('二级服务菜单', r)
      //   that.setData({
      //     'multiObjArray[1]': r.data.data,
      //     cat_id: r.data.data[0].id
      //   })
      // });

    })
  },

  setm(e) {
    let that = this;
    let pid = e.currentTarget.dataset.id;
    let title = e.currentTarget.dataset.title;
    let catObj = {
      id: '',
      title: ''
    }
    catObj.id = pid;
    catObj.title = title;
    console.log('catObj:', catObj)

    this.data.catArr.push(catObj)
    console.log('catidArr:', this.data.catArr);

    that.setData({
      catArr: this.data.catArr
    })

    console.log('pid', pid)
    let params = {
      type: this.data.type,
      pid: pid,
      vollege_id: app.globalData.vollege_id
    }

    util.apiRequest(ServiceMenu, params, null).then(r => {
      console.log('二级服务菜单', r)
      if (r.data.data.length == 0) {
        console.log('没有下级菜单')
        that.setData({
          showView: true
        });
        return;
      }
      that.setData({
        allMenu: r.data.data
      })
    });
  },

  // bindMultiPickerChange: function(e) {

  //   console.log('picker改变，携带值为', e.detail.value)
  //   let multiIndex = e.detail.value;
  //   let catpid = this.data.multiObjArray[0][multiIndex[0]].id
  //   let cat_id = this.data.multiObjArray[1][multiIndex[1]].id
  //   console.log('大id', catpid)
  //   console.log('小id', cat_id)
  //   this.setData({
  //     multiIndex: e.detail.value,
  //     catpid: catpid,
  //     cat_id: cat_id,
  //     isChecked: true
  //   })
  // },

  // bindMultiPickerColumnChange: function(e) {
  //   let that = this;
  //   console.log('修改的列为', e.detail.column, '，值为', e.detail.value);

  //   if (e.detail.column == 0) {
  //     console.log('111', that.data.multiObjArray)
  //     let pid = that.data.multiObjArray[0][e.detail.value].id;
  //     console.log('二级pid', pid)
  //     let params = {
  //       type: this.data.type,
  //       pid: pid
  //     }
  //     util.apiRequest(ServiceMenu, params, null).then(r => {
  //       console.log('切换是二级服务菜单：', r)
  //       that.setData({
  //         'multiObjArray[1]': r.data.data
  //       })
  //     });

  //   }
  // },

  inputMobile(e){ // 输入电话
    this.setData({
      mobile: e.detail.value
    })
  },

  inputContent(e) {
    this.setData({
      content: e.detail.value
    })
  },

  close: function() { // 关闭弹窗
    let that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },

  getTmplIds() { // 获取模板消息id集合
    let that = this;
    util.apiRequest(GetTemplateConfig, {
      vollege_id: app.globalData.vollege_id
    }, null).then(r => {
      console.log('模板id：', r)
      let tmplIds = r.data.data.map(item => {
        return item.value.template_id
      })
      console.log('66tmplIds:', tmplIds)
      that.setData({
        tmplIds
      })
    })
  },

  onSubmit() {
    let that = this;

    if (that.data.catArr.length <2){
      wx.showModal({
        showCancel: false,
        content: '请选择服务菜单',
      })
      return;
    }

    if (this.data.curIndex == 1 && this.data.tname == '') {
      wx.showModal({
        showCancel: false,
        content: '请填写他人姓名',
      })
      return;
    }

    console.log('this.data.mobile:',this.data.mobile)
    console.log('777:',/^1[3456789]\d{9}$/.test(this.data.mobile))

    if (!(/^1[3456789]\d{9}$/.test(this.data.mobile))){
      wx.showModal({
        showCancel: false,
        content: '请填写正确的手机号',
      })
      return;
    }

    let content = this.data.content.replace(/(^\s*)|(\s*$)/g, "");
    if (!content) {
      wx.showModal({
        showCancel: false,
        content: '请填写服务内容',
      })
      return;
    }

    if (this.data.curIndex == 1) {
      var params = {
        uid: that.data.uid,
        catpid: that.data.catArr[0].id,
        cat_id: that.data.catArr[1].id,
        content: that.data.content,
        tid: that.data.tid,
        t_name: that.data.tname,
        vollege_id: app.globalData.vollege_id,
        town_id: this.data.member.town_id,
        mobile:that.data.mobile
      }
    } else {
      var params = {
        uid: that.data.uid,
        catpid: that.data.catArr[0].id,
        cat_id: that.data.catArr[1].id,
        content: that.data.content,
        vollege_id: app.globalData.vollege_id,
        town_id: this.data.member.town_id,
        mobile: that.data.mobile
      }
    }

    console.log('333', params)
    util.apiRequest(UpVolunt, params).then(r => {
      console.log('提交：', r)
      wx.requestSubscribeMessage({
        tmplIds: that.data.tmplIds,
        success(res) {
          console.log('订阅成功', res)
        }
      })
      if (r.data.code == 1) {
        wx.showModal({
          showCancel: false,
          content: r.data.info,
          success(res) {
            if (res.confirm) {
              wx.redirectTo({
                url: "/pages/culture/cultureList/cultureList"
              })
            }
          }
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})